package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketAdmin;
import com.cskaoyan.market.service.AdminAdminService;
import com.cskaoyan.market.service.impl.AdminAdminServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @description: 负责管理员账户管理
 * @author: 刘国兴
 **/
@WebServlet("/admin/admin/*")
public class AdminAdminController extends HttpServlet {

    private AdminAdminService adminService = new AdminAdminServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        dispatchRequest(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        dispatchRequest(req, resp);
    }

    /**
     * @author 刘国兴
     * @description url分发到不同的方法, 使不同的请求都可以使用
     */
    private void dispatchRequest(HttpServletRequest req, HttpServletResponse resp) {
        String op = req.getPathInfo();
        try {
            switch (op) {
                case "/list":
                    //列表显示与查询功能
                    getAdminList(req, resp);
                    break;
                case "/create":
                    //新增用户
                    createAdmin(req, resp);
                    break;
                case "/update":
                    //编辑用户
                    updateAdmin(req, resp);
                    break;
                case "/delete":
                    //删除用户
                    deleteAdmin(req, resp);
                    break;
                default:
                    //重定向交给其他servlet处理这个请求
                    resp.sendRedirect(req.getRequestURI());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author 刘国兴
     * @description 列表显示与查询功能
     */
    private void getAdminList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //第一步：获取req中&连接的参数page&limit&username&sort&order
        Map<String, String[]> parameterMap = req.getParameterMap();
        //第二步：调用service处理业务，并把参数传递下去，获取adminList
        List<MarketAdmin> adminList = adminService.list(parameterMap);
        //第三步：处理成标准响应的格式然后响应回去
        Object okList = ResponseUtil.okList(adminList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(okList));
    }

    /**
     * @author 刘国兴
     * @description 新增用户
     */
    private void createAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取req中的json字符串
        String reqJson = req.getReader().readLine();
        //调用service处理业务
        String respJson = adminService.create(reqJson);
        //响应
        resp.getWriter().println(respJson);

    }

    /**
     * @author 刘国兴
     * @description 编辑用户
     */
    private void updateAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取req中的json字符串
        String reqJson = req.getReader().readLine();
        //调用service处理业务
        String respJson = adminService.update(reqJson);
        //响应
        resp.getWriter().println(respJson);
    }

    /**
     * @author 刘国兴
     * @description 删除账户
     */
    private void deleteAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取req中的json字符串
        String reqJson = req.getReader().readLine();
        //调用service处理业务
        String respJson = adminService.delete(reqJson);
        //响应
        resp.getWriter().println(respJson);
    }
}
